package visao.converter;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import controle.ControleEquipe;
import modelo.Equipe;

@FacesConverter("equipeConverter")
public class EquipeConverter  implements Converter {
		
	    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
	        if(value != null && value.trim().length() > 0) {
	        		ControleEquipe controleEquipe = new ControleEquipe();
	            try {
	                return controleEquipe.buscarPorId(Long.parseLong(value));
	            } catch(Exception e) {
	                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid theme."));
	            }
	        }
	        else {
	            return null;
	        }
	    }
	 
	    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
	        if(object != null&& object.getClass().equals(Equipe.class)) {
	            return String.valueOf(((Equipe) object).getId());
	        }
	        else {
	            return null;
	        }
    }   
}         

